package com.zlaidyrac.weride.ui;

import java.util.ArrayList;
import java.util.HashMap;

import com.zlaidyrac.weride.main.Common;
import com.zlaidyrac.weride.main.R;
import com.zlaidyrac.weride.webclient.WebClient;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class TabsActivity extends TabActivity {
	Thread notificationThread;
	private Handler handler = new Handler();
	int mId = 1;
	HashMap<String, String> params;
	ConnectivityManager connectivityManager;
	NetworkInfo activeNetworkInfo;
	
	/**
	 * Method to check whether there is an active network connection available
	 * @return boolean true if network available
	 */
	public boolean isNetworkAvailable() {
		connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetworkInfo != null;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		setContentView(R.layout.activity_tab);

		TabHost tabHost = getTabHost();

		// Tab for Photos
		TabSpec poolMyCar = tabHost.newTabSpec("Pool My Car");
		// setting Title and Icon for the Tab
		poolMyCar.setIndicator("Advertise",
				getResources().getDrawable(R.drawable.cartab));
		Intent poolMyCarIntent = new Intent(this, PoolMyCar.class);
		poolMyCar.setContent(poolMyCarIntent);

		// Tab for Songs
		TabSpec findARide = tabHost.newTabSpec("Find a Ride");
		findARide.setIndicator("Search",
				getResources().getDrawable(R.drawable.ic_action_search));
		Intent findARideIntent = new Intent(this, Ride.class);
		findARide.setContent(findARideIntent);

		// Tab for Videos
		TabSpec myStats = tabHost.newTabSpec("Me");
		myStats.setIndicator("Me", getResources().getDrawable(R.drawable.stat));
		Intent myStatsIntent = new Intent(this, MyStats.class);
		myStats.setContent(myStatsIntent);

		// Adding all TabSpec to TabHost
		tabHost.addTab(poolMyCar); // Adding poolmycar tab
		tabHost.addTab(findARide); // Adding finda ride tab
		tabHost.addTab(myStats); // Adding myStat tab
		params = new HashMap<String, String>();
		params.put("user_id", Common.loggedUser.getUserId());
		// Notifications thread
		Thread notificationThread = new Thread() {
			@Override
			public void run() {
				while (true) {
					if ( !isNetworkAvailable() ) {
						continue;
					}
					ArrayList<HashMap<String, String>> notifications = WebClient.postRequestForArrayList("notifications",
									params);
					
					if ( !notifications.isEmpty() ) {
						for (HashMap<String, String> item : notifications) {
							NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
									getApplicationContext())
									.setSmallIcon(R.drawable.cartab)
									.setContentTitle("WeRide")
									.setContentText(item.get("message"));
							Intent resultIntent = new Intent(getApplicationContext(),com.zlaidyrac.weride.ui.ShowPassengerActivity.class);
							resultIntent.putExtra("user_id", item.get("related_id"));
							TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());

							stackBuilder.addParentStack(TabsActivity.class);

							stackBuilder.addNextIntent(resultIntent);
							PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
											PendingIntent.FLAG_UPDATE_CURRENT);
							mBuilder.setContentIntent(resultPendingIntent);
							NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
							// mId allows you to update the notification later
							// on.
							mNotificationManager.notify(new Integer(item.get("id")), mBuilder.build());
						}
					}
					try {
						Thread.sleep(5000);
					}
					catch( InterruptedException ie ) {
						ie.printStackTrace();
					}
					//handler.postDelayed(this, 5000);
					
				}
			}
		};
		notificationThread.start();
	}
	@Override
	protected void onResume() {
		super.onResume();
		handler.removeCallbacks(notificationThread);
		handler.postDelayed(notificationThread, 0);
	}
	@Override
	protected void onPause() {
		super.onPause();
		handler.removeCallbacks(notificationThread);
	}
}